﻿OB.Emes_ActionButton = OB.Emes_ActionButton || {};

//质检判定
var QualityDataCollectionView_Init_Data = {
	title: "质检判定",
	width: '80%',
	height: '80%',
	align: 'center',
	defaultLayoutAlign: 'center',
	allItem: [],
	processData:{},
	submit: function(isForce){
		var _self = this;
		if(null === this.orderProcessForm.getValue("code")){
			isc.showMessage("MES批次号/花篮ID不能为空！", isc.OBMessageBar.TYPE_INFO);
			return ;
		}
		if(null === this.orderInfoForm.getValue("emesoManuBatchId")){
			isc.showMessage("没有当前生产订单号信息！", isc.OBMessageBar.TYPE_INFO);
			return ;
		}
		if("选择" === this.dataCollectionInfo.getValue("result")){
			isc.showMessage("质检结果不能为空！", isc.OBMessageBar.TYPE_INFO);
			return ;
		}
		if("REWORK" === this.dataCollectionInfo.getValue("result") && null === this.dataCollectionInfo.getValue("r_emesProcessId")){
			isc.showMessage("返工工序不能为空！", isc.OBMessageBar.TYPE_INFO);
			return ;
		}
		if("选择" === this.dataCollectionInfo.getValue("poorReason")){
			isc.showMessage("不良原因不能为空！", isc.OBMessageBar.TYPE_INFO);
			return ;
		}
		
		isc.confirm("确定提交质检结果信息？", function(bool){
			if (bool) {
				var requestData = {
					method: "saveQualityResultInfo",
					code: _self.orderProcessForm.getValue("code"),
					emesoManuBatchId: _self.orderInfoForm.getValue("emesoManuBatchId"),
					result: _self.dataCollectionInfo.getValue("result"),
					r_emesProcessId: _self.processData[_self.dataCollectionInfo.getValue("r_emesProcessId")],
					reason: _self.dataCollectionInfo.getValue("poorReason"),
					description: _self.dataCollectionInfo.getValue('description')
				};
				_self.submitLoading = true;
				OB.RemoteCallManager.call("com.mes.handler.quality.EmesqQualityCheckHandler", requestData, {},
					function(response, data, request){
						_self.submitLoading = false;
						if(data.result){
							_self.clearForm();
							isc.showMessage("提交成功！", isc.OBMessageBar.TYPE_INFO);
						}else{
							isc.showMessage(data.msg, isc.OBMessageBar.TYPE_ERROR);
						}
					}
				);
			}
		});
	},
	initWidget: function (data) {
		this.entityName = data._entityName;
		
		this.height = document.body.clientHeight * 0.8;
		this.width = document.body.clientWidth * 0.8;
		//OB控件 追加	addField or addItem	会有样式和事件找不到的问题
		this.createOrderInfoForm(data);
		this.createForm();
		var pageLayout = isc.VLayout.create({
			width: '100%',
			height: this.height,
			layoutMargin : 5,
			membersMargin : 10,
			members : [
				this.jobOrderInfoLayout,
				this.dataCollectionInfo,
				this.buttonLayout
			]
		});
		
		//却别弹框与直接菜单打开方式
		if (data && data.code) {
			this.items = [pageLayout];
		} else {
			this.addMembers(pageLayout);
		}
		this.Super("initWidget", arguments);
		
		if (data && data.code) {
			this.orderData = data;
		} else {
			this.clearForm();
		}
		
		this.initProcessInfo();
	},
	createOrderInfoForm: function() {
		var _self = this;
		this.orderProcessForm = isc.DynamicForm.create({
			ID: "orderProcessForm",
			width: '50%',
			numCols : 2,
			fields: [
				{width: '*',type: 'text', title: 'MES批次号/花篮ID',name: 'code', canEdit: true, startRow: false, 
					changed:function(form, item, value){
						if('undefined' !== typeof(value)){
							OB.RemoteCallManager.call("com.mes.handler.quality.EmesqQualityCheckHandler",
									{method:"loadEmesoManuBatchInfoByInConveyanceID",code:value},{},
									function(response, data, request) {
										if('undefined' !== typeof(data.dataInfo)){
											_self.setValue(data.dataInfo, _self.orderInfoForm);
										}
									}
							);
						}
					}
				}
			]
		});
		this.orderInfoForm = isc.DynamicForm.create({
			ID: "orderInfoForm",
			width: '100%',
			numCols : 6,
			fields : [
				{width: '*',name:'group',type: 'OBSectionItem',defaultValue:'订单信息',sectionExpanded: true,colSpan: 6,itemIds: 
					['manuBatchCode','processName','productCode','productName','startTime','endTime','qty', 'description']},
				{type:'hidden',name:"emesoManuBatchId"},	
				{width: '*',type: 'text', title:'生产批次',	name: 'manuBatchCode',	canEdit:false, startRow: false, readOnlyDisplay: "static"},
				{width: '*',type: 'text', title:'当前工序',	name: 'processName',	canEdit:false, startRow: false, readOnlyDisplay: "static"},
				{width: '*',type: 'text', title:'物料编号',	name: 'productCode',	canEdit:false, startRow: true, readOnlyDisplay: "static"},
				{width: '*',type: 'text', title:'物料名称',	name: 'productName',	canEdit:false, startRow: false, readOnlyDisplay: "static"},
				{width: '*',type: 'text', title:'计划开始日期',name: 'startTime',    canEdit:false, startRow: true, readOnlyDisplay: "static"},
				{width: '*',type: 'text', title:'计划结束日期',name: 'endTime',		canEdit:false, startRow: false, readOnlyDisplay: "static"},
				{width: '*',type: 'integer', title:'计划数量', name: 'qty',		canEdit:false, startRow: false, readOnlyDisplay: "static"},
				{width: '*',type: 'textarea',title:'备注', name: 'description', width: '100%', height: 20, colSpan: 6, canEdit: false, startRow: true, readOnlyDisplay: "static"}
			]
		});
		
		this.createFileButton();
		
		this.jobOrderInfoLayout = isc.HLayout.create({
			width: '100%',
			layoutMargin: 0,
			memberMargin: 5,
			members:[
			 	isc.VLayout.create({
					width: '85%',
					layoutMargin: 0,
					memberMargin: 0,
					align: 'left',
					defaultLayoutAlign: 'left',
					members:[
						this.orderProcessForm,
					 	this.orderInfoForm
					]
				}),
			 	isc.VLayout.create({
					width: '15%',
					layoutMargin: 15,
					memberMargin: 30,
					align: 'left',
					defaultLayoutAlign: 'left',
					members:[
						this.processStandardBtn,
						this.productPictureBtn,
						this.jobInstructionsBtn
					]
				})
			]
		});
	},
	createFileButton: function() {
		this.processStandardBtn = isc.OBFormButton.create({
			ID:'processStandardBtn',
			title : '工艺标准',
			action : function() {
				OB.RemoteCallManager.call("com.mes.handler.ViewDrawingsOrderHandler",
					{processId: _self.processId},
					{},
					function(response, data, request) {
						if(data.flag){
							isc.EMES_Window_View.create({url: "DownFileServlet.html?path=" + encodeURIComponent(data.meg) + "&online=true&fileName=" + _self.routeNo + ".pdf"}).show();
						}else{
							top.isc.showMessage("该作业订单工序未上传工序标准。",top.isc.OBMessageBar.TYPE_SUCCESS);
						}
					}
				);
			}
		});
		this.productPictureBtn = isc.OBFormButton.create({
			ID:'productPictureBtn',
			title : '产品图纸',
			action : function() {
				OB.RemoteCallManager.call("com.mes.handler.ViewmaterialOrderHandler",
					{processId: _self.processId},
					{},
					function(response, data, request) {
						if(data.flag){
							isc.EMES_Window_View.create({url: "DownFileServlet.html?path=" + encodeURIComponent(data.meg) + "&online=true&fileName=" + _self.routeNo + ".pdf"}).show();
						}else{
							isc.showMessage(data.message);
						}
					}
				);
			}
		});
		this.jobInstructionsBtn = isc.OBFormButton.create({
			ID:'jobInstructionsBtn',
			title : '作业指导书',
			action : function() {
				OB.RemoteCallManager.call("com.mes.handler.ViewprocessOrderHandler",
					{processId: _self.processId},
					{},
					function(response, data, request) {
						if(data.flag){
							isc.EMES_Window_View.create({url: "DownFileServlet.html?path=" + encodeURIComponent(data.meg) + "&online=true&fileName=" + _self.routeNo + ".pdf"}).show();
						}else{
							isc.showMessage(data.message);
						}
					}
				);
			}
		});
	},
	createForm: function() {
		var _self = this;
		this.dataCollectionInfo = isc.DynamicForm.create({
			ID: "dataCollectionInfo",
			width:'85%',
			numCols : 6,
			fields : [
				{type: 'OBSectionItem', name: 'group', defaultValue: '录入质检信息',sectionExpanded: true,itemIds: 
					['result','poorReason','description','prompt'], colSpan: 6},
				{width: '*',type: 'select', title:'质检结果',defaultValue:'选择',name: 'result', startRow: true, canEdit: true,
					changed:function(form, item, value){
						if("REWORK" === value){
							_self.dataCollectionInfo.getField("r_emesProcessId").show();
						}else{
							_self.dataCollectionInfo.getField("r_emesProcessId").hide();
						}
					}
				},
				{width: '*',type: 'select', title:'不良原因',defaultValue:'选择',name: 'poorReason', startRow: false, canEdit: true},
				{width: '*',type: 'select', title:'返工工序',defaultValue:'选择',name: 'r_emesProcessId', startRow: false, canEdit: true},
				{width: '*',type: 'textarea', title:'备注', colSpan: 6, name:'description', height: 50, startRow: true, canEdit: true},
				{width: '*',type: 'textarea', title:'提示', colSpan: 6, name:'prompt', height: 50, startRow: true, canEdit: false}
			]
		});
		this.submitOkBtn = isc.OBFormButton.create({
			ID: 'submitOkBtn',
			title: '提交',
			action: function() {
				_self.submit(false);
			}
		});
		
		this.submitDelBtn = isc.OBFormButton.create({
			ID: 'submitDelBtn',
			title: '重置',
			action: function() {
				_self.clearForm();
			}
		});
		this.buttonLayout = isc.HLayout.create({
			width: '100%',
			layoutMargin: 15,
			memberMargin: 30,
			align: 'center',
			defaultLayoutAlign: 'center',
			members:[
		 		this.submitOkBtn,
				this.submitDelBtn
			]
		});
	},
	clearForm: function(){
		this.clearDCItemForm();//清除表单二选项
		this.setValue({}, orderInfoForm);
	},
	setValue: function(data, form){
		var itemIds = form.getFields();
		for(var i = 0 ; i < itemIds.length; i ++){
			var column = itemIds[i];
			this.setFieldValue(form.getField(column.name), data[column.name]);
		}
	},
	setFieldValue: function(field, value) {
		if (field.readOnlyDisplay = "static") {
			field.setReadOnlyDisplay("");
			field.setValue(value);
			field.setReadOnlyDisplay("static");
		} else {
			field.setValue(value);
		}
	},
	clearDCItemForm: function(){
		var group = dataCollectionInfo.getItem("group").itemIds;
		for(var i = group.size()-1 ; i >= 0 ; i --){
			if(group[i].indexOf("_item_") == 0 ){
				dataCollectionInfo.removeItem(dataCollectionInfo.getItem(group[i]));
				group.remove(group[i]);
			}
		}
	},
	initProcessInfo: function(){
		var _self = this; 
		OB.RemoteCallManager.call("com.mes.handler.quality.EmesqQualityCheckHandler",
				{method:"loadPoorReasonInfo"},{},
				function(response, data, request) {
					var selectData = {};
					for(var i=0;i<data.dataInfo.length;i++){
						selectData[data.dataInfo[i].code]=data.dataInfo[i].name;
					}
					_self.dataCollectionInfo.getField("poorReason").setValueMap(selectData);
				}
		);
		OB.RemoteCallManager.call("com.basis.handler.GetReferenceListHandler",
				{id: "0B80A589DFD64147AD0126954CE05B8A"},
				{},
				function(response, data, request) {
					if(data.result){
						_self.resultMap = {};
						for (var key in data.list) {
							_self.resultMap[key] = data.list[key];
						}
						_self.dataCollectionInfo.getField("result").setValueMap(_self.resultMap);
					}else{
						isc.showMessage(data.text, isc.OBMessageBar.TYPE_ERROR);
					}
				}
		);
		OB.RemoteCallManager.call("com.mes.handler.quality.EmesqQualityDataCollectionHandler",
				{method:"loadProcessDataInfo"},{},
				function(response, data, request) {
					var selectData = [];
					for(var i=0;i<data.dataInfo.length;i++){
						_self.processData[data.dataInfo[i]['emesProcessName']] = data.dataInfo[i]['emesProcessId'];
						selectData.push(data.dataInfo[i]['emesProcessName']);
					}
					_self.dataCollectionInfo.getField("r_emesProcessId").setDefaultValue(data.defaultValue);
					_self.dataCollectionInfo.getField("r_emesProcessId").setValueMap(selectData);
					_self.dataCollectionInfo.getField("r_emesProcessId").hide();
				}
		);
	}
};

isc.defineClass("EMESQ_QualityCheckView", isc.OBBaseView).addProperties(QualityDataCollectionView_Init_Data);